package cn.pn.project.mapper.user;

import cn.pn.project.pojo.user.User;
import org.apache.ibatis.annotations.*;
import java.sql.SQLException;
import java.util.List;

/**
 * 用户
 */
@Mapper
public interface UserDao {
    /**
     * 更新用户余额
     * @return
     */
    @Update("UPDATE  `pn_user`.`user` SET `balance`=#{balance} where  `userId`=#{userId} and `status`=1;")
    Integer updateUserBalance(@Param("balance") Double balance,@Param("userId") Integer userId);
    /**
     * 更新用户积分
     * @return
     */
    @Update("UPDATE  `pn_user`.`user` SET `integral`=#{integral} where  `userId`=#{userId} and `status`=1;")
    Integer updateUserIntegral(@Param("integral") Integer integral,@Param("userId") Integer userId);

    /**
     * 用户插入
     * @param user
     * @return
     * @throws SQLException
     */
    @Insert("INSERT INTO `pn_user`.`user` (  `nickName`,  `signature`,  `userPwd`,  `telephone`,  `headImgURL`,  `status`,  `userIdentity`)VALUES  (   #{u.nickName},   #{u.signature},   #{u.userPwd},   #{u.telephone},   #{u.headImgURL},   #{u.status},   #{u.userIdentity}  );")
    Integer insertUser(@Param("u") User user) throws SQLException;

    /**
     * 用户更新
     * @param user
     * @return
     * @throws SQLException
     */
    @Update("UPDATE  `pn_user`.`user` SET `nickName`=#{u.nickName},`signature`=#{u.signature},`userPwd`=#{u.userPwd},`sex`=#{u.sex},`telephone`=#{u.telephone},`headImgURL`=#{u.headImgURL},`status`=#{u.status},`registTime`=#{u.registTime},`loginTime`=#{u.loginTime},`IDCard`=#{u.IDCard},`userIdentity`=#{u.userIdentity},`more1`=#{u.more1},`more2`=#{u.more2},`more3`=#{u.more3}  where  `userId`=#{u.userId} and `status`=1;" )
    Integer updateUser(@Param("u") User user) throws SQLException;

    /**
     * 用户删除
     * @param uid
     * @return
     * @throws SQLException
     */
    @Delete("DELETE  `pn_user`.`user`  WHERE  `userId`=#{uid};")
    Integer DeleteUser(@Param("uid") Integer uid) throws SQLException;

    /**
     * 查询所有用户
     * @param user 查询条件 可以id，可以组合，动态sql生成
     * @return
     * @throws SQLException
     */
    @Select("<script>" +
            " select  `userId`,  `nickName`,  `signature`,  `userPwd`,  `sex`,  `telephone`,  `headImgURL`,  `status`,  `registTime`,  `loginTime`,  `IDCard`,`userIdentity`,  `more1`,  `more2`,  `more3` , `balance`,`integral`\n" +
            "        from  pn_user.`user`  u\n" +
            "        <where>\n" +
            "            <if test='u.userId!=null and u.userId!=0'>\n" +
            "                AND  u.userId = #{u.userId}\n" +
            "            </if>\n" +
            "            <if test='u.nickName!=null and u.nickName!=\"\" '>\n" +
            "                AND u.nickName like concat('%',#{u.nickName},'%')\n" +
            "            </if>\n" +
            "            <if test='u.signature!=null and u.signature != \"\"'>\n" +
            "                AND  u.signature like concat('%',#{u.signature},'%')\n" +
            "            </if>\n" +
            "            <if test='u.userPwd!=null and u.userPwd!=\"\"'>\n" +
            "                AND  u.userPwd = #{u.userPwd}\n" +
            "            </if>\n" +
            "            <if test='u.sex!=null and u.sex!=0'>\n" +
            "                AND  u.sex = #{u.sex}\n" +
            "            </if>\n" +
            "            <if test='u.telephone!=null and u.telephone!=\"\"'>\n" +
            "                AND  u.telephone = #{u.telephone}\n" +
            "            </if>\n" +
            "            <if test='u.status!=null and u.status!=0'>\n" +
            "                AND  u.status = #{u.status}\n" +
            "            </if>\n" +
            "            <if test='u.IDCard !=null and u.IDCard!=\"\"'>\n" +
            "                AND u.IDCard=#{u.IDCard}\n" +
            "            </if>\n" +
            "            <if test='u.userIdentity!=null and u.userIdentity!=\"\"'>\n" +
            "                AND u.userIdentity = #{u.userIdentity}\n" +
            "            </if>\n" +
            "               and `status`=1    " +
            "       </where>\n" +
           /* "\n" +
            "        <if test='index!=null and index!=0 and size!=null and size!=\"\"'>\n" +
            "            limit #{index},#{size}\n" +
            "        </if>" +*/
            "</script>")
    //@Results({@Result(column = "", property = "", one = @One(select = "")), @Result(column = "", property = "", many = @Many(select = ""))})
    List<User> selectAllUser(@Param("u") User user/*,@Param("index") Integer index,@Param("size") Integer size*/) throws SQLException;

    /**
     * 更新指定用户的最后登陆时间
     * @param userId
     * @return
     */
    @Update("UPDATE  `pn_user`.`user`SET  `loginTime` = NOW() WHERE `userId` = #{userId} and `status`=1;")
    Integer updateLoginTimeByUserId(@Param("userId")Integer userId);
}
